
#include "member_BLayout.h"
#include "member_BStyle.h"

using namespace BWE;

member_BLayout::member_BLayout(BLayout* layout)
{
	boss = layout;
	host = 0;
	part = Part_None;
	parent = 0;
	minSize.set(0, 0);
	maxSize.set(999999, 999999);
	visible = true;
	dirty = true;
}
member_BLayout::~member_BLayout()
{

}

void member_BLayout::setHost(BWidget* newHost)
{
	if (host != newHost)
	{
		if (host)
		{
			boss->detached(host);
			boss->emit(Signal_Detached, BValue(newHost, true));
		}
		host = newHost;
		if (host)
		{
			boss->attached(newHost);
			boss->emit(Signal_Attached, BValue(newHost, true));
		}
	}
}

void member_BLayout::dispenseUpperStyles(BLayout* upper)
{
	if (BLayout* parent = layout_member(upper)->parent)
	{
		dispenseUpperStyles(parent);
	}
	if (BStyle* style = layout_member(upper)->realStyle)
	{
		style_member(style)->apply(boss, parent == upper);
	}
}
void member_BLayout::dispenseStyles(BStyle* hostStyle, BEvent& event)
{
	if (realStyle)
	{
		realStyle->clearAll();
		realStyle->clearAnnexes();
		realStyle->clearChildren();
	}

	style_member(hostStyle)->apply(boss, parent == 0);
	if (parent)
		dispenseUpperStyles(parent);

	if (realStyle)
	{
		event.setValue(realStyle);
		boss->event(event);
	}
	for (int i = 0; i < boss->count(); i++)
	{
		if (BLayout* child = boss->layout(i))
		{
			layout_member(child)->dispenseStyles(hostStyle, event);
		}
	}
}
void member_BLayout::dispenseEvent(const BEvent& event)
{
	for (int i = 0; i < boss->count(); i++)
	{
		BLayout* child = boss->layout(i);
		if (!child)
			continue;
		layout_member(child)->dispenseEvent(event);
		if (event.accepted())
			return;
	}
	boss->event(event);
}
